// 传入一个file对象, 将其转为base64后返回的函数
import fs from 'fs'
import path from 'path'

export function fileToBase64(file: File): Promise<string> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = () => {
      resolve(reader.result as string)
    }
    reader.onerror = (error) => {
      reject(error)
    }
  })
}

export const generateTree = (dir: string) => {
  const items = fs.readdirSync(dir, { withFileTypes: true })
  return (
    items
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      ?.map((item) => {
        const fullPath = path.join(dir, item.name)
        if (item.isDirectory()) {
          return {
            title: item.name,
            key: fullPath,
            children: generateTree(fullPath)
          }
        } else if (item.isFile()) {
          return {
            title: item.name,
            key: fullPath,
            isLeaf: true //后续根据这个判断是不是文件
          }
        }
      })
      .filter(Boolean)
  )
}
